package com.cswangdao.controller.user;

import com.cswangdao.domain.model.MarketCoupon;
import com.cswangdao.domain.vo.AvailableCouponVo;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/coupon")
@Slf4j
public class WxCouponController {


    @Autowired
    private CouponService couponService;

    @GetMapping("/list")
    public Result<PageResult<MarketCoupon>> list(int page,int limit){
        log.info("获取所有优惠券列表：page={},limit={}",page,limit);
        PageResult<MarketCoupon> marketCoupons = couponService.allCoupons(page,limit);
        return Result.success(marketCoupons);
    }

    @PostMapping("receive")
    public Result receive(@RequestBody Map params) {
        log.info("领取优惠券：{}", params);
        couponService.receiveCoupon(params);
        return Result.success();
    }

    @GetMapping("/mylist")
    public Result<PageResult<MarketCoupon>> myCoupons(short status,int page,int limit){
        log.info("获取我的优惠券列表：status={},page={},limit={}",status,page,limit);
        PageResult<MarketCoupon> pageResult = couponService.myCoupons(status,page,limit);
        return Result.success(pageResult);
    }

    @GetMapping("/selectlist")
    public Result<PageResult<AvailableCouponVo>> selectAvailableCoupons(int cartId){
        log.info("获取可用优惠卷");
        PageResult<AvailableCouponVo> pageResult = couponService.selectAvailableCoupons(cartId);
        return Result.success(pageResult);
    }


    @PostMapping("/exchange")
    public Result exchange(@RequestBody Map map){
        log.info("兑换优惠券：{}", map);
        couponService.exchangeCoupon(map.get("code").toString());
        return Result.success();
    }
}
